package com.gym.tygglxt.service.impl;

import com.gym.tygglxt.mapper.*;
import com.gym.tygglxt.pojo.*;
import com.gym.tygglxt.service.EquipmentService;
import com.gym.tygglxt.util.DateConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class EquipmentServiceImpl implements EquipmentService {
    @Autowired
    private EquipmentCustomMapper equipmentCustomMapper;

    @Autowired
    private EquipmentPriceMapper equipmentPriceMapper;

    @Autowired
    private EquipmentMapper equipmentMapper;

    @Autowired
    private EquipmentStatusMapper equipmentStatusMapper;

    @Autowired
    private EquipmentRentMapper equipmentRentMapper;

    @Autowired
    private EquipmentRentCustomMapper equipmentRentCustomMapper;

    @Autowired
    private EquipmentServiceMapper equipmentServiceMapper;

    @Autowired
    private EquipmentServiceCustomMapper equipmentServiceCustomMapper;

    @Override
    public List<EquipmentCustom> queryEqu() {
        List<EquipmentCustom> equipmentCustomList = equipmentCustomMapper.queryEqu();
        return equipmentCustomList;
    };

    @Override
    public EquipmentCustom searchEquById(int equId) {
        EquipmentCustom equipmentCustom = equipmentCustomMapper.selectByPrimaryKey(equId);
        return equipmentCustom;
    };

    @Override
    public List<EquipmentCustom> queryEquByExample(EquipmentCustomExample equipmentCustomExample) {
        List<EquipmentCustom> equipmentCustomList = equipmentCustomMapper.queryEquByExample(equipmentCustomExample);
        return equipmentCustomList;
    }

    @Override
    public List<EquipmentPrice> queryPriceByExample(EquipmentPriceExample equipmentPriceExample) {
        List<EquipmentPrice> equipmentPriceList = equipmentPriceMapper.selectByExample(equipmentPriceExample);
        return equipmentPriceList;
    }

    @Override
    public Boolean insertEquipment(Equipment equipment) {
        try {
            EquipmentStatus equipmentStatus = new EquipmentStatus();
            equipmentStatus.setEqustatus("1");
            equipmentMapper.insert(equipment);
            equipmentStatusMapper.insert(equipmentStatus);
            return true;
        } catch (Exception e) {
            System.out.println(e);
            return false;
        }
    }

    @Override
    public Boolean insertEquPrice(EquipmentPrice equipmentPrice) {
        try {
            equipmentPriceMapper.insert(equipmentPrice);
            return true;
        } catch (Exception e) {
            System.out.println(e);
            return false;
        }
    }

    @Override
    public Boolean delEquipment(int equId) {
        try {
            equipmentMapper.deleteByPrimaryKey(equId);
            equipmentStatusMapper.deleteByPrimaryKey(equId);
            return true;
        } catch (Exception e) {
            System.out.println(e);
            return false;
        }
    }

    @Override
    public Boolean delEquPrice(String equType) {
        try {
            equipmentPriceMapper.deleteByPrimaryKey(equType);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Boolean updateEqu(Equipment equipment) {
        try {
            equipmentMapper.updateByPrimaryKey(equipment);
            return true;
        } catch (Exception e) {
            System.out.println(e);
            return false;
        }
    }

    @Override
    public List<EquipmentRentCustom> queryEquRent(EquipmentRentCustomExample equipmentRentCustomExample) {
        try {
            List<EquipmentRentCustom> equipmentRentCustomList = equipmentRentCustomMapper.selectByExample(equipmentRentCustomExample);
            return equipmentRentCustomList;
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }

    @Override
    public Boolean rentEqu(int equId, String userId) {
        try {
            DateConverter dateConverter = new DateConverter();
            EquipmentRent equipmentRent = new EquipmentRent();
            equipmentRent.setEquid(equId);
            equipmentRent.setUserid(userId);
            equipmentRent.setRentstartdate(new Date());
            equipmentRent.setRentenddate(dateConverter.convert("1970-01-01"));
            equipmentRent.setRentstatus(0);
            equipmentRentMapper.insert(equipmentRent);
            EquipmentStatus equipmentStatus = new EquipmentStatus();
            equipmentStatus.setEquid(equId);
            equipmentStatus.setEqustatus("0");
            equipmentStatusMapper.updateByPrimaryKey(equipmentStatus);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Boolean returnEqu(int rentId, String userId) {
        try {
            EquipmentRent equipmentRent = equipmentRentMapper.selectByPrimaryKey(rentId);
            equipmentRent.setRentenddate(new Date());
            equipmentRent.setRentstatus(1);

            EquipmentStatus equipmentStatus = equipmentStatusMapper.selectByPrimaryKey(equipmentRent.getEquid());
            equipmentStatus.setEquid(equipmentRent.getEquid());
            if (!equipmentStatus.getEqustatus().equals("2")) {
                equipmentStatus.setEqustatus("1");
            }

            equipmentRentMapper.updateByPrimaryKey(equipmentRent);
            equipmentStatusMapper.updateByPrimaryKey(equipmentStatus);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Boolean delEquRent(int rentId) {
        try {
            equipmentRentMapper.deleteByPrimaryKey(rentId);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<EquipmentServiceCustom> queryEquService(EquipmentServiceCustomExample equipmentServiceCustomExample) {
        try {
            List<EquipmentServiceCustom> equipmentServiceCustomList = equipmentServiceCustomMapper.selectByExample(equipmentServiceCustomExample);
            return equipmentServiceCustomList;
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }

    @Override
    public Boolean serviceEqu(int equId, String userId) {
        try {
            DateConverter dateConverter = new DateConverter();

            com.gym.tygglxt.pojo.EquipmentService equipmentService = new com.gym.tygglxt.pojo.EquipmentService();
            equipmentService.setEquid(equId);
            equipmentService.setUserid(userId);
            equipmentService.setServicestartdate(new Date());
            equipmentService.setServiceenddate(dateConverter.convert("1970-01-01"));
            equipmentService.setServicestatus(0);

            EquipmentStatus equipmentStatus = new EquipmentStatus();
            equipmentStatus.setEquid(equId);
            equipmentStatus.setEqustatus("2");

            equipmentServiceMapper.insert(equipmentService);
            equipmentStatusMapper.updateByPrimaryKey(equipmentStatus);

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Boolean finishEqu(int serviceId, String userId) {
        try {
            com.gym.tygglxt.pojo.EquipmentService equipmentService = equipmentServiceMapper.selectByPrimaryKey(serviceId);
            equipmentService.setServicestatus(1);
            equipmentService.setServiceenddate(new Date());

            EquipmentStatus equipmentStatus = equipmentStatusMapper.selectByPrimaryKey(equipmentService.getEquid());
            equipmentStatus.setEqustatus("1");

            equipmentServiceMapper.updateByPrimaryKey(equipmentService);
            equipmentStatusMapper.updateByPrimaryKey(equipmentStatus);

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<String> getAllEquType() {
        try {
            List<EquipmentPrice> equipmentPriceList = equipmentPriceMapper.selectByExample(null);
            List<String> equTypeList = new ArrayList<>();

            for (EquipmentPrice equipmentPrice : equipmentPriceList) {
                equTypeList.add(equipmentPrice.getEqutype());
            }

            return equTypeList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Boolean updateEquPrice(EquipmentPrice equipmentPrice) {
        try {
            equipmentPriceMapper.updateByPrimaryKey(equipmentPrice);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
